package com.study.repository;

import com.study.dto.NamesOnly;
import com.study.dto.StuInferDTO;
import com.study.dto.StuLomDTO;
import com.study.dto.StudentDTO;
import com.study.entity.Student;
import java.util.Collection;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Classname UsersDao
 * @Version 1.0.0
 * @Date 2022/4/2 23:31
 * @Created by yd
 */
@Repository
public interface StudentRepository extends BaseRepository<Student,Long> {

    @Query(value = "from Student where name=:name")
    List<Student> getStudent(@Param("name") String name);

    @Transactional
    @Modifying
    @Query(value = "update Student t set t.name=:name where t.id=:id")
    int updateStudent(@Param("name") String name,@Param("id") Long id);

    // QueryByExampleExecutor

    /**
     * 无法使用select * ，使用* 无法自动驼峰匹配
     * @return
     */
    @Query(value = "select  id,name, birth_time birthTime from student  ", nativeQuery = true)
    List<StuInferDTO> listDTO();

    @Query(value = "select  * from student", nativeQuery = true)
    <T> List<T> listStuDTO(Class<T> type);

    @Query(value = "select name firstname,concat(id,name) lastname from STUDENT",nativeQuery = true)
    List<NamesOnly> listNameOnly();
}
